'use strict';
const unipay = require('uni-pay');
const configCenter = require("uni-config-center");
// 读取配置中心配置
var unipayConfig = configCenter({
	pluginId: 'uni-pay'
}).requireFile('config.js');
// 读取配置中心配置
var wxConfig = configCenter({
	pluginId: 'wx-login'
}).requireFile('config.js');

exports.main = async (event, context) => {
	console.log('xxf-uniPay event:', event);
	const {
		action,
	} = event;
	switch (action) {
		case 'getOrderInfo':
			return getOrderInfo(event);
		case 'orderQuery':
			return orderQuery(event);
		case 'orderRefund':
			return orderRefund(event);
		default:
			return {
				msg: 'Invalid action！',
					code: 0,
					data: null
			};
	}
}

async function orderRefund(params) {
	console.log('orderRefund:', params);
	const {
		transactionId,
		outTradeNo,
		outRefundNo,
		totalFee,
		refundFee,
	} = params;
	const unipayIns = unipay.initWeixin({
		appId: wxConfig.appId,
		mchId: unipayConfig.wxpay.mp.mchId,
		key: unipayConfig.wxpay.mp.key,
		pfx: unipayConfig.wxpay.mp.pfx,
	});
	console.log('unipayIns:', unipayIns);
	if (unipayIns) {
		let refundRes = undefined;
		try {
			// 必须云端环境才可以
			refundRes = await unipayIns.refund({
				transactionId,
				outTradeNo,
				outRefundNo, // 商户退款单号
				totalFee, // 订单总金额
				refundFee, // 退款总金额
			});
		} catch (e) {
			refundRes = e;
		}
		console.log('refundRes:', refundRes);

		if (refundRes && refundRes.resultCode === "SUCCESS") {
			return {
				msg: '订单已退款！',
				code: 1,
				data: {
					...refundRes
				}
			}
		} else {
			return {
				msg: '取消失败或其它情况！',
				code: 0,
				data: null
			}
		}
	}
}

async function orderQuery(params) {
	console.log('orderQuery:', params);
	const {
		transactionId,
		outTradeNo,
	} = params;
	try {
		const unipayIns = unipay.initWeixin({
			appId: wxConfig.appId,
			mchId: unipayConfig.wxpay.mp.mchId,
			key: unipayConfig.wxpay.mp.key,
		});
		if (unipayIns) {
			let queryRes = null;
			if (transactionId) {
				queryRes = await unipayIns.orderQuery({
					transactionId,
				});
			} else {
				queryRes = await unipayIns.orderQuery({
					outTradeNo,
				});
			}
			console.log('try queryRes:', JSON.stringify(queryRes));
			if (queryRes && queryRes.tradeState === "SUCCESS") {
				return {
					msg: '订单已支付！',
					code: 1,
					data: {
						...queryRes
					}
				}
			} else {
				return {
					msg: '未支付或其它情况！',
					code: 0,
					data: null
				}
			}
		}
	} catch (err) {
		console.log("orderQuery Err: ", err);
		return {
			msg: '订单不存在或其它情况！',
			code: 0,
			data: null
		}
	}
}

async function getOrderInfo(params) {
	console.log('getOrderInfo:', JSON.stringify(params));
	const {
		openId,
		orderNo,
		money,
		title
	} = params;
	const unipayIns = unipay.initWeixin({
		appId: wxConfig.appId,
		mchId: unipayConfig.wxpay.mp.mchId,
		key: unipayConfig.wxpay.mp.key,
	});
	if (unipayIns) {
		const payInsRes = await unipayIns.getOrderInfo({
			openid: openId,
			body: title,
			outTradeNo: orderNo,
			totalFee: money,
			notifyUrl: unipayConfig.notifyUrl,
		});
		console.log('getOrderInfo:', JSON.stringify(payInsRes));

		if (payInsRes && payInsRes.paySign) {
			return {
				msg: 'success',
				code: 1,
				data: {
					...payInsRes
				},
			}
		} else {
			return {
				msg: '支付失败！',
				code: 2,
				data: null,
			}
		}
	} else {
		return {
			msg: '支付失败！',
			code: 2,
			data: null,
		}
	}
};